package com.ruoyi.factorydata.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.MaterialData;
import com.ruoyi.factorydata.domain.ProductionBom;
import com.ruoyi.factorydata.domain.dto.ProductionBomDTO;
import com.ruoyi.factorydata.domain.vo.MaterialDataVo;
import com.ruoyi.factorydata.domain.vo.ProductionBomVo;
import com.ruoyi.factorydata.service.IMaterialDataService;
import com.ruoyi.factorydata.service.IProductionBomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品bomController
 * 
 * @author ruoyi
 * @date 2025-07-20
 */
@RestController
@RequestMapping("/productionbom/productionBom")
public class ProductionBomController extends BaseController
{
    @Autowired
    private IProductionBomService productionBomService;

    @Autowired
    private IMaterialDataService materialDataService;

    /**
     * 查询产品bom列表
     */
    @PreAuthorize("@ss.hasPermi('productionbom:productionBom:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductionBom productionBom)
    {
        startPage();
        List<ProductionBomVo> list = productionBomService.selectProductionBomList(productionBom);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

    @PostMapping("/getProductionList")
    public TableDataInfo getProductionList(@RequestBody ProductionBom productionBom) {
        startPage();
        List<ProductionBom> list = productionBomService.list(new QueryWrapper<ProductionBom>().lambda()
                .eq(productionBom.getId() != null,ProductionBom::getId, productionBom.getId())
                .like(productionBom.getMaterialName() != null,ProductionBom::getMaterialName, productionBom.getMaterialName())
                .eq(ProductionBom::getStatus,"1"));
        return getDataTable(list);
    }

    /**
     * 获取物料数据类型不为原材料的数据
     */

    @GetMapping("/getMaterialDataExcludeType")
    public AjaxResult getMaterialDataExcludeType(@RequestParam(value = "materialId", required = false) Long id,
                                                 @RequestParam(value = "materialName", required = false) String materialName){
        startPage();
        List<MaterialData> list = materialDataService.lambdaQuery()
                .in(MaterialData::getMaterialType, 4,5)
                .eq(MaterialData::getMaterialStatus, 1)
                .eq(id != null, MaterialData::getId, id)
                .like(materialName != null && !materialName.isBlank(),MaterialData::getMaterialName, materialName)
                .list();
        return AjaxResult.success(list);
    }

    /**
     * 查询物料不包括自己的数据
     */
    @GetMapping("/getMaterialDataExcludeId")
    public AjaxResult getMaterialDataExcludeId(@RequestParam(value = "materialCode", required = false) String materialCode,
                                               @RequestParam(value = "materialName", required = false) String materialName,
                                               @RequestParam(value = "materialId", required = false) Long id,
                                               @RequestParam(value = "materialType",required = false)String materialType){
        if (id == null){
            return AjaxResult.error("请先选择物料");
        }
        startPage();
        MaterialData materialData = new MaterialData();
        materialData.setMaterialName(materialName);
        materialData.setMaterialCode(materialCode);
        materialData.setMaterialType(materialType);
        materialData.setId(id);
        List<MaterialDataVo> list = materialDataService.selectMaterialDataListExcludeId(materialData);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return AjaxResult.success(tableDataInfo);
    }
    /**
     * 导出产品bom列表
     */
    @PreAuthorize("@ss.hasPermi('productionbom:productionBom:export')")
    @Log(title = "产品bom", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductionBom productionBom)
    {
        List<ProductionBomVo> list = productionBomService.selectProductionBomList(productionBom);
        ExcelUtil<ProductionBomVo> util = new ExcelUtil<ProductionBomVo>(ProductionBomVo.class);
        util.exportExcel(response, list, "产品bom数据");
    }

    /**
     * 获取产品bom详细信息
     */
    @PreAuthorize("@ss.hasPermi('productionbom:productionBom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productionBomService.selectProductionBomById(id));
    }

    /**
     * 新增产品bom
     */
    @PreAuthorize("@ss.hasPermi('productionbom:productionBom:add')")
    @Log(title = "产品bom", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionBomDTO productionBomDTO)
    {
        productionBomDTO.setCreateBy(getUserId().toString());
        return toAjax(productionBomService.insertProductionBom(productionBomDTO));
    }

    /**
     * 修改产品bom
     */
    @PreAuthorize("@ss.hasPermi('productionbom:productionBom:edit')")
    @Log(title = "产品bom", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody  ProductionBomDTO productionBomDTO)
    {

        productionBomDTO.setCreateBy(getUserId().toString());
        return toAjax(productionBomService.updateProductionBom(productionBomDTO));
    }

    /**
     * 更新产品bom
     */
    @PreAuthorize("@ss.hasPermi('productionbom:productionBom:updateStatus')")
    @RequestMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody ProductionBomDTO productionBomDTO){
        boolean update = productionBomService.update(new UpdateWrapper<ProductionBom>().lambda()
                .eq(ProductionBom::getId, productionBomDTO.getId())
                .set(ProductionBom::getStatus, productionBomDTO.getStatus()));
        return toAjax(update);
    }
    /**
     * 删除产品bom
     */
    @PreAuthorize("@ss.hasPermi('productionbom:productionBom:remove')")
    @Log(title = "产品bom", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productionBomService.deleteProductionBomByIds(ids));
    }
}
